import {GCtrl} from "../../lib/util/GCtrl";

const {ccclass, property} = cc._decorator;

@ccclass
export default class DWC_BindPassword extends cc.Component {

    @property(cc.EditBox)
    pwEdit: cc.EditBox = null;

    @property(cc.EditBox)
    pwSureEdit: cc.EditBox = null;

    @property(cc.Node)
    pwSeeClose:cc.Node = null;

    @property(cc.Node)
    pwSureSeeClose:cc.Node = null;

    @property(cc.Node)
    lookNormal:cc.Node = null;

    @property(cc.Node)
    lookSureNormal:cc.Node = null;


    @property(cc.Label)
    lbPwCanNotSee:cc.Label = null;
    @property(cc.Label)
    lbPwSureCanNotSee:cc.Label = null;

    canSeePwSure = true;
    canSeePw = true;


    btnClickSure(){

        if(this.pwEdit.string == ""){
            GCtrl.GLUtils.showTips("输入正确的密码");return false;
        }

        if(this.pwEdit.string.length <  8){
            GCtrl.GLUtils.showTips("输入正确的密码");return false;
        }

        if(!this.checkIsSavePassWord() ){
            GCtrl.GLUtils.showTips("两次输入的密码不一样");
            return;
        }
        let cmd = "ChangeLoginPasswd";
        let msg: any = {
            login_passwd:this.pwSureEdit.string,
            old_passwd:"",
            type:2,     //验证码修改
        }
        GCtrl.GLUtils.sendRequest(cmd, msg, (error, data) => {
            if (data.errcodedes)
                GCtrl.GLUtils.showTips(data.errcodedes);
            else {
                GCtrl.GLUtils.showTips("密码绑定成功");
                GCtrl.UIMgr.close(GCtrl.GLConfig.DWC_BindPasswrod_path, GCtrl.UIMgr.closeType.destory);
                //绑定代理
                this.checkParentRid();
            }
        });

    }

    checkParentRid(){
        //绑定代理
        if(!cc.vv.session.getParentRid()){ // 没有直属代理  提示绑定
            cc.vv.session.getIsAgent((isAgent)=>{
                if(!isAgent){
                    cc.vv.UIMgr.open(cc.vv.GLConfig.FPJ_BindShangJi);
                }
            },()=>{
                //这个时候断网，就没法获取，也默认打开界面，不允许操作
                cc.vv.UIMgr.open(cc.vv.GLConfig.FPJ_BindShangJi);
            })
        }
    }

    checkIsSavePassWord(){

        let str1 = this.pwEdit.string;
        let str2 = this.pwSureEdit.string;
        if(str1 == str2 ){
            return true;
        }else {
            return false;
        }
    }

    setLookSureState(){
        if(!this.canSeePwSure){
            this.pwSureSeeClose.active = true;
            this.lookSureNormal.active = false;
            this.pwSureEdit.textLabel.node.opacity = 0;
            this.pwSureEdit.textLabel.node.scale = 0;

            this.lbPwSureCanNotSee.node.active = true;
            this.lbPwSureCanNotSee.string = this.getPwSureStringCanNotSee();
        }else {
            this.pwSureEdit.textLabel.node.opacity = 255;
            this.pwSureEdit.textLabel.node.scale = 1;

            this.lbPwSureCanNotSee.node.active = false;
            this.pwSureSeeClose.active = false;
            this.lookSureNormal.active = true;
        }
    }
    setLookState(){
        if(!this.canSeePw){
            this.pwSeeClose.active = true;
            this.lookNormal.active = false;
            this.pwEdit.textLabel.node.opacity = 0;
            this.pwEdit.textLabel.node.scale = 0;


            this.lbPwCanNotSee.node.active = true;
            this.lbPwCanNotSee.string = this.getPwStringCanNotSee();
        }else {
            this.pwEdit.textLabel.node.opacity = 255;
            this.pwEdit.textLabel.node.scale = 1;

            this.lbPwCanNotSee.node.active = false;
            this.pwSeeClose.active = false;
            this.lookNormal.active = true;
        }
    }

    getPwStringCanNotSee(){
        let str = this.pwEdit.string;
        let strCanNotSee = "";
        for(let i = 0 ; i < str.length ; i++){
            strCanNotSee += "*";
        }
        return strCanNotSee;
    }
    getPwSureStringCanNotSee(){
        let str = this.pwSureEdit.string;
        let strCanNotSee = "";
        for(let i = 0 ; i < str.length ; i++){
            strCanNotSee += "*";
        }
        return strCanNotSee;
    }

    pwTextChange(){
        // this.lbPwCanNotSee.string = this.getPwStringCanNotSee();
        this.setLookState();
    }
    pwSureTextChange(){
        //this.lbPwCanNotSee.string = this.getPwSureStringCanNotSee();
        this.setLookSureState();
    }

    onClickCanSeePw(){
        this.canSeePw = !this.canSeePw;
        this.setLookState();
    }

    onClickCanSeePwSure(){
        this.canSeePwSure = !this.canSeePwSure;
        this.setLookSureState();
    }
}
